<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      :inline="true"
      label-width="68px"
      class="query-form"
    >
      <el-form-item label="隧道区间" prop="sn">
        <el-select v-model="queryParams.sn" placeholder="请选择隧道区间">
          <el-option
            v-for="section in tnnelSection"
            :key="section.sn"
            :label="section.name"
            :value="section.sn"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="handleQuery">搜索</el-button>
        <el-button size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="content-container height_100-query_from">
      <div class="handle_bar">
        <div class="tit"><span>设备列表</span></div>
      </div>
      <div class="flex-1" style="position: relative;overflow: hidden;">
        <div style="position: absolute;left: 0;top: 0;bottom: 0;right: 0;display: flex;flex-direction: column;overflow: hidden;">
          <el-table
            v-loading="loading"
            class="flex-1"
            height="250"
            style="width:100%;overflow: hidden;"
            :data="equList"
          >
            <el-table-column label="设备名称" align="center" prop="name" />
            <el-table-column label="类型" align="center" prop="deviceType">
              <template slot-scope="scope">
                <span v-if="scope.row.deviceType == 'ammeter'">控制器</span>
                <span v-if="scope.row.deviceType == 'camera'">摄像头</span>
                <span v-if="scope.row.deviceType == 'radar'">雷达</span>
                <span v-if="scope.row.deviceType == 'mem'">电表</span>
              </template>
            </el-table-column>
            <el-table-column label="设备编号" align="center" prop="sn" />
            <el-table-column label="设备通道号" align="center" prop="code" />
            <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  v-hasPermi="['lc:equ:edit']"
                  size="mini"
                  type="text"
                  @click="handleView(scope.row)"
                >查看</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <DeviceDetails ref="deviceDetails" />
  </div>
</template>

<script>
import { listEqu, getEqu, delEqu, addEqu, updateEqu } from '@/api/fjgs/equ'
import { listAll } from '@/api/system/tunnel'
import { getTnnelSection, getDeviceBySection, deviceDetails } from '@/api/fjgs/light'
import DeviceDetails from './components/DeviceDetails.vue'

export default {
  components: {
    DeviceDetails
  },
  name: 'Equ',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备定义表格数据
      equList: [],
      tnnelSection: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        sn: ''
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
    getTnnelSection().then(res => {
      this.tnnelSection = res.data
    })
  },
  methods: {
    /** 查询设备定义列表 */
    getList() {
      this.loading = true
      getDeviceBySection(this.queryParams).then(response => {
        response.data.ammeter.map(e => e.deviceType = 'ammeter')
        response.data.camera.map(e => e.deviceType = 'camera')
        response.data.mem.map(e => e.deviceType = 'mem')
        response.data.radar.map(e => e.deviceType = 'radar')
        this.equList = [
          ...response.data.ammeter,
          ...response.data.camera,
          ...response.data.mem,
          ...response.data.radar
        ]
        // this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        equName: null,
        locationName: null,
        produceCompany: null,
        supplier: null,
        brand: null,
        price: null,
        produceDate: null,
        warranty: null,
        ip: null,
        lightType: null,
        deviceType: 2
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 查看 */
    handleView(row) {
      deviceDetails({
        sn: row.sn,
        code: row.code,
        deviceType: row.deviceType
      }).then(response => {
        const details = {
          deviceType: row.deviceType,
          ...response.data[0]
        }
        this.$refs.deviceDetails.open(details)
        // this.open = true
        // this.title = '修改设备定义'
      })
    }
  }
}
</script>

